package com.example.SocialApplication.activity.things;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Base64;
import android.view.View;
import android.widget.*;
import com.example.SocialApplication.Daemon;
import com.example.SocialApplication.HelperFactory;
import com.example.SocialApplication.R;
import com.example.SocialApplication.activity.BaseActivity;
import com.example.SocialApplication.activity.dialog.ChooseIconThingDialog;
import com.example.SocialApplication.activity.dialog.ColorSpectrumThingDialog;
import com.example.SocialApplication.activity.dialog.ColorSpectrumThingInterface;
import com.example.SocialApplication.activity.dialog.IconThingInterface;
import com.example.SocialApplication.db.entity.Thing;
import com.example.SocialApplication.db.entity.ThingMain;
import com.example.SocialApplication.db.entity.User;
import com.example.SocialApplication.service.CallServiceGetThingImages;

import java.io.File;
import java.sql.SQLException;
import java.util.List;

public class AddNewThingActivity extends BaseActivity implements ColorSpectrumThingInterface, IconThingInterface {

    private User user;
    private LinearLayout backgroundLayout;
    private ChooseIconThingDialog mIconDialog;
    private ColorSpectrumThingDialog mColorDialog;
    private List<String> mListIconNames;
    private ImageView mIconImage;
    private TextView mIconName;

    private String mColorItem;
    private String mNameItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_new_thing);

        user = Daemon.get("user", User.class);

        backgroundLayout = (LinearLayout) findViewById(R.id.add_new_thing_background_layout);

        Button buttonColorGamma = (Button) findViewById(R.id.add_new_thing_button_color_gamma);  /// todo slot for this button
        buttonColorGamma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseColorSpectrumEventClick(v);
            }
        });

        Button buttonChooseIcon = (Button) findViewById(R.id.add_new_thing_button_icon);
        buttonChooseIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chooseIconThing();
            }

        });
        Button buttonSave = (Button) findViewById(R.id.add_new_thing_button_save);
        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onCLickSave();
            }
        });

        mIconImage = (ImageView) findViewById(R.id.add_new_thing_icon_preview);

        TextView textView = (TextView) findViewById(R.id.add_new_thing_user_name);
        textView.setText(user.getFirstName() + " " + user.getLastName());

        ImageView userAvatar = (ImageView) findViewById(R.id.add_new_thing_user_avatar);
        byte[] decodedString = Base64.decode(user.getPhoto(), Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        userAvatar.setImageBitmap(decodedByte);

        mIconName = (TextView) findViewById(R.id.add_new_thing_icon_name);

        EditText editText = (EditText) findViewById(R.id.add_new_thing_edit_name);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                mIconName.setText(editable);
            }
        });

        new CallServiceGetThingImages(){
            @Override
            protected void onPostExecute(List<String> strings) {
                mListIconNames = strings;
            }
        }.execute();
    }

    private void onCLickSave() {
        ThingMain thing = new ThingMain();
        thing.setName(mIconName.getText().toString());
        thing.setPictureId(mNameItem);
        thing.setColor(mColorItem);

        try {
            HelperFactory.getHelper().getThingMainDAO().create(thing);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        setResult(10);
        finish();
    }

    private void chooseIconThing() {
        mIconDialog = new ChooseIconThingDialog(this, mListIconNames, this);
        mIconDialog.show();
    }

    public void chooseColorSpectrumEventClick(View v) {
        mColorDialog = new ColorSpectrumThingDialog(this, (ImageView) findViewById(R.id.add_new_thing_icon_preview), this);
        mColorDialog.show();
    }

    public void saveThingEventCLick(View v) {
//        ImageView view = (ImageView) findViewById(R.id.imageView);
//        Drawable drawable = view.getDrawable();
//        view.setImageDrawable(adjust(drawable));
        //finish();
    }

    @Override
    public void onSpectrumClick(String color) {
        mColorItem = color;
        backgroundLayout.setBackgroundColor(Color.parseColor(color));
        mColorDialog.dismiss();
    }

    private Bitmap getBitmap(String name) {
        String pathToFolder = getExternalCacheDir().getAbsolutePath()
                + File.separator
                + "images"
                + File.separator;

        String fileName = File.separator + name + ".png";
        String path = pathToFolder + fileName;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap result = BitmapFactory.decodeFile(path, options);
        return result;
    }

    @Override
    public void onIconCLick(String name) {
        mNameItem = name;
        mIconImage.setImageBitmap(getBitmap(name));
        mIconDialog.dismiss();
    }
}
